package com.util;

//import com.dao.UserinfoDao;
//import com.pojo.Userinfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

public class MyBatisHelp {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "MyBatisConfig.xml"; //配置文件
        InputStream inputStream = null;
        try{
            inputStream = Resources.getResourceAsStream(resource);
            //从xml配置文件中创建SqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
//    @Test
//    public void testMain(){
//        SqlSession session = getSession();
//        UserinfoDao userinfoDao = session.getMapper(UserinfoDao.class);
//        Userinfo userinfo = userinfoDao.queryById(1L);
//        System.out.println(userinfo);
//    }
}
